<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class QuestionsApiController extends AbstractRestfulController
{
    public function getList()
    {			
		header('Access-Control-Allow-Origin: *');
		$questionsTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$questionsList = $questionsTable->getTotalQuestions();
		if(($questionsList->count())!=0){
		$data = array();$i=0;
				foreach($questionsList as $questions){
					$qid=$questions->question_id;
					$sid=$questions->subject_id;
					$cid=$questions->course_id;
					$data[$i]['sno']=$i+1;
					$data[$i]['question_name']= $questions->question_name;
					$data[$i]['level']= $questions->level;
					$data[$i]['action']='<a href="javascript:void(0)" onclick = "editQuestionFunction('.$sid.','.$cid.','.$qid.')">Edit</a> &nbsp;/&nbsp;<a href="javascript:void(0);" onClick="deleteQuestion('.$sid.','.$cid.','.$qid.')">Delete</a>';
					$i++;
				}
				$data['aaData'] = $data;
				echo json_encode($data['aaData']); exit;
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	 =>	1,
			));
		}	
    }
    public function get($course_id)
    {	
		header('Access-Control-Allow-Origin: *');
		$questionsTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$resultsTable=$this->getServiceLocator()->get('Models\Model\ResultsFactory');
		$explodeId=explode('-',$course_id);
			$courseId=$explodeId[0];
			$value=$explodeId[1];
			$user_id=$explodeId[2];
			$level=$explodeId[3];
			$subject_id=$explodeId[4];
			$question_id=$explodeId[8];
		if($explodeId[5]!=0){
			$questionsList = $resultsTable->getResultsQuestionslist($explodeId[5],$explodeId[6],$explodeId[7],$user_id)->toarray();
		}else{
			if($user_id==0){
				$questionsList = $questionsTable->getQuestion($courseId)->toarray();
			}else{
				if($question_id==0){
					$questionsList = $questionsTable->getQuestionslist($courseId,$value,$user_id,$level,$subject_id,$question_id)->toArray();
				}else{
					$questionsList = $questionsTable->getSingQuestion($courseId,$value,$user_id,$level,$subject_id,$question_id)->toArray();
				}
			}
		}		
		if(count($questionsList)!=0){
			return new JsonModel(array(
				'questionsList' 	=> json_encode($questionsList),
				'value'	=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	=>	0,
			));
		}	
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');
		$questionTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$answerTable=$this->getServiceLocator()->get('Models\Model\AnswersFactory');
		if($data['question_id']==0){
			$saveQuestions  = $questionTable->saveQuestions($data);
			$saveAnswer		= $answerTable->saveAnswer($saveQuestions,$data);
		}else{
			$saveQuestions	= $questionTable->upadteQuestions($data);
			$saveAnswer		= $answerTable->updateAnswer($data);
		}
		return new JsonModel(array(
				'questionId' 	=> $saveQuestions,
				'value'			=>	1,
			));
    }
    public function update($type,$data)
    {
		header('Access-Control-Allow-Origin: *');	
		$questionTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$interviewQuestionsList = $questionTable->getTotalInterviewQuestions($type);
		if(($interviewQuestionsList->count())!=0){
		$data = array();$i=0;
				foreach($interviewQuestionsList as $questions){
					$qid=$questions->question_id;
					$sid=$questions->subject_id;
					$cid=$questions->course_id;
					$data[$i]['sno']=$i+1;
					$data[$i]['question_name']= $questions->question_name;
					$data[$i]['level']= $questions->level;
					$data[$i]['action']='<a href="javascript:void(0)" onclick = "editQuestionFunction('.$sid.','.$cid.','.$qid.')">Edit</a> &nbsp;/&nbsp;<a href="javascript:void(0);" onClick="deleteQuestion('.$sid.','.$cid.','.$qid.')">Delete</a>';
					$i++;
				}
				$data['aaData'] = $data;
				echo json_encode($data['aaData']); exit;
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	 =>	1,
			));
		}	
    }
    public function delete($ids)
    {
		$explode_id=explode('-',$ids);
       $questionTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$deleteTech = $questionTable->deleteQuestion($explode_id[0],$explode_id[1],$explode_id[2]);
			if($deleteTech!=0){
				return new JsonModel(array(
					'status'		=>	'success',
					'value'			=>	1,
				));	
			}else{
				return new JsonModel(array(
					'status'	=>	'fail',
					'value'		=>   0,
				));			
			}
    }
}